package oocl.ita.eordering.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;

import oocl.ita.eordering.po.Food;
import oocl.ita.eordering.po.Merchant;
import oocl.ita.eordering.po.Order;
import oocl.ita.eordering.service.FoodService;
import oocl.ita.eordering.service.MerchantService;
import oocl.ita.eordering.service.OrderService;
import oocl.ita.eordering.util.JSONUtil;

@Controller(value = "/operate")
public class ClientOperationController {

	@Autowired(required = true)
	private MerchantService mService;
	@Autowired(required = true)
	private FoodService fService;
	@Autowired(required = true)
	private OrderService oService;

	@RequestMapping("/showMechant")
	public String showMechant(HttpServletRequest request) {
		List<Merchant> merchants = mService.findAllMerchant();
		request.getSession().setAttribute("merchants", merchants);
		if (merchants != null) {
			for (Merchant merchant : merchants) {
				System.out.println(merchant.getmPersonName());
			}
			return "index";
		} else {
			return "error";
		}
	}

	@RequestMapping("/showFood")
	public String showFood(int mId, HttpServletRequest request) {
		List<Food> foods = fService.findFoodByMerchantId(mId);
		request.getSession().setAttribute("foods", foods);
		if (foods != null) {
			for (Food food : foods) {
				System.out.println(food.getmId());
				System.out.println(food.getfId());
			}
			return "index";
		} else {
			return "error";
		}
	}

	@RequestMapping("/showOrder")
	public String showOrder(int cId, HttpServletRequest request) {
		List<Order> orders = oService.showOrderByClientId(cId);
		request.getSession().setAttribute("orders", orders);
		if (orders != null) {
			for (Order order : orders) {
				System.out.println(order.getoId());
			}
			return "suc";
		} else {
			return "error";
		}
	}
	
	@RequestMapping("/addOrder")
	@ResponseBody
	public List<Order> addOrder(Order order){
		oService.addOrder(order);
		List<Order> orders=oService.showOrderByClientId(order.getcId());
//		String orderJson=JSONUtil.objetc2Json(orders);
		return orders;
		
	}

}
